(function($){
	
	$(function() {
		
		//修改用户
		$('#update-userinfo').dialog({
            autoOpen: false,
            height: 350,
            width: 420,
            modal: true,
            buttons: {
                '保存': function(){
        			var dept=$('#depart2').val();
        			var userName=$('#username2').val();
        			var adminType=$('#accounttype2').val();
        			var flag = $("#updateForm").validate(VALIDATE.updateAccount).form();
        			if(flag){
        				 $.ajax({
  		            	   type: "POST",
  		            	   url: $('#updateForm').attr("action"),
  		            	   data: "dept="+dept+"&username="+userName+"&adminType="+adminType,
  		            	   success: function(msg){
  		            		  if(msg.startWith("error:")){
  		            			  showTips("更新出错了:"+msg.substring(6,msg.length));
  		           			  }else{
  		           				showTips("执行成功",function(){
  		           					window.location.reload();
  		           				},
  		           				function(){
  		           					window.location.reload();	
  		           				}
  		           				);
  		           			  }
  		            	   }
  		            	});
        			}
		           
                },
				'取消': function(){
					$(this).dialog('close');
				}
            }
        });
		
		//点击修改
		$('.update-userinfo-link').click(function(e){
			e.preventDefault();
			var ele=e.target;
			var aid= ele.getAttribute('aid');
			var login_name= ele.getAttribute('login_name');
			var username= ele.getAttribute('username');
			var dept= ele.getAttribute('dept');
			var type= ele.getAttribute('type');
			$('#accountname2').html(login_name);
			$('#depart2').val(dept);
			$('#username2').val(username);
			$('#accounttype2').val(type);
			$('#update-userinfo').dialog('open');
			$('#updateForm').attr("action",ctx+"/account/updateAdmin.do?aid="+aid)
		});
		
		//增加用户
		$('#add-userinfo').dialog({
            autoOpen: false,
            height: 350,
            width: 420,
            modal: true,
            buttons: {
                '保存': function(){
                	var accountname1=$('#accountname1').val();
                	var depart1=$('#depart1').val();
                	var username1=$('#username1').val();
                	var adminType1=$('#accounttype1').val();
                	var flag = $("#addAccountForm").validate(VALIDATE.addAccount).form();
                	if(flag){
                		$.ajax({
 		            	   type: "POST",
 		            	   url: ctx+"/account/checkAccountLoginName.do" ,
 		            	   data: "accountname="+accountname1,
 		            	   success: function(msg){
 		            		  if(msg.startWith("error:")){
 		            			  showTips("出错了:"+msg.substring(6,msg.length));
 		           			  }else{
 		           				$.ajax({
 		 		            	   type: "POST",
 		 		            	   url: ctx+"/account/saveAdmin.do" ,
 		 		            	   data: "accountname1="+accountname1+"&depart1="+depart1+"&username1="+username1+"&adminType1="+adminType1,
 		 		            	   success: function(msg){
 		 		            		  if(msg.startWith("error:")){
 		 		            			 showTips("出错了:"+msg.substring(6,msg.length));
 		 		           			  }else{
 		 		           				showTips("执行成功",function(){
 		 		           					window.location.reload();
 		 		           				},
 		 		           				function(){
 		 		           					window.location.reload();
 		 		           				});
 		 		           				
 		 		           			  }
 		 		            	   }
 		 		            	});
 		           			  }
 		            	   }
 		            	});
                	}
                },
				'取消': function(){
					$(this).dialog('close');
				}
            }
        });
		
		$('.add-userinfo-link').click(function(e){
			e.preventDefault();
			$('#accountname1').val("");
			$('#depart1').val("");
			$('#depart1').val("");
			$('#accounttype1').val("");
			$('#add-userinfo').dialog('open');
		});
		
		//全选
		$('.checked-all').click(function () {
			var _cbs = $(this).closest('.content').find('table input:checkbox');
			_cbs.prop('checked', this.checked);
		});
		
		$('.content table input:checkbox').click(function() {
			var _content = $(this).closest('.content');
			var _cbs = _content.find('table input:checkbox');
			_content.find('input:checkbox').last().prop('checked' , _cbs.length === _cbs.filter(':checked').length ? true : false);
		});
		
		//删除提示
		$('.is-delete').click(function(){
			var array = checkedToArray('adminId');
			if(array.length==0){
				showTips('请选择要删除的账户');
			}else{
				showTips('确定要删除吗？', function() {
					//取参数
				
					//提出异步请求
					 $.ajax({
						 url: ctx+'/account/removeAdmins.do',
						 type: "post",
						 data: {
							 chks:array.toString()
						 },
						 success: function(msg) {
							 if(msg.startWith("error:")){
								 showTips("出错了:"+msg.substring(6,msg.length));
			           			  }else{
			           				showTips("执行成功",function(){
			           					window.location.reload();
			           				},
			           				function(){
			           					window.location.reload();
			           				}
			           				);
			           			  }
						 }
					 });
				});
			}
		})
		
		
		//锁定
		$('.is-lock').click(function(){
			var array = checkedToArray('adminId');
			if(array.length==0){
				showTips('请选择要锁定的账户');
			}else{
				showTips('确定要锁定吗？', function() {
					//取参数
					
					//提出异步请求
					 $.ajax({
						 url: ctx+'/account/updateLock.do',
						 type: "post",
						 data: {
							 chks:array.toString(),
							 pTag:"2"
						 },
						 success: function(msg) {
							 if(msg.startWith("error:")){
								 showTips("出错了:"+msg.substring(6,msg.length));
			           			  }else{
			           				showTips("执行成功",function(){window.location.reload();},
			           						function(){
			           					window.location.reload();
			           				});
			           				
			           			  }
						 }
					 });
				});
			}
			
		})
		
		//解锁
		$('.is-unlock').click(function(){
			var array = checkedToArray('adminId');
			if(array.length==0){
				showTips('请选择要解锁的账户');
			}else{
				showTips('确定要解锁吗？', function() {
					//提出异步请求
					 $.ajax({
						 url: ctx+'/account/updateLock.do',
						 type: "post",
						 data: {
							 chks:array.toString(),
							 pTag:"3"
						 },
						 success: function(msg) {
							 if(msg.startWith("error:")){
								 showTips("出错了:"+msg.substring(6,msg.length));
			           			  }else{
			           				showTips("执行成功",function(){window.location.reload();},
			           						function(){
			           					window.location.reload();
			           				});
			           				
			           			  }
						 }
					 });
				});
			}
			
		})
		
		
		//重置密码
		$('.is-reset-password').click(function(e){
			e.preventDefault();
			var ele=e.target;
			var aid= ele.getAttribute('aid');
			 $.ajax({
				 url: ctx+'/account/restorePassword.do',
				 type: "post",
				 data: {
					 adminId:aid
				 },
				 success: function(msg) {
					 if(msg.startWith("error:")){
						 showTips("出错了:"+msg.substring(6,msg.length));
	           			  }else{
	           				showTips("执行成功",function(){window.location.reload();},
	           						function(){
	           					window.location.reload();
	           				});
	           			  }
				 }
			 });
		
		});
		
		//查询账户
		$('#searchAccountButton').click(function(e){
			var flag = $("#searchAccountForm").validate(VALIDATE.searchAccount).form();
			if(flag){
				$("#searchAccountForm").submit();
			}
		});
	})
	
})(jQuery);